<template>
  <div class="process-control-container">
    <el-card>
      <template #header>
        <div class="card-header">
          <span>过程检验</span>
          <div class="header-operations">
            <el-button type="primary" @click="handleCreate">新建检验</el-button>
            <el-button type="success" @click="handlePlan">检验计划</el-button>
          </div>
        </div>
      </template>

      <el-form :inline="true" :model="queryForm" class="search-form">
        <el-form-item label="工单号">
          <el-input
            v-model="queryForm.workOrderNo"
            placeholder="请输入工单号"
          />
        </el-form-item>
        <el-form-item label="产品编码">
          <el-input
            v-model="queryForm.productCode"
            placeholder="请输入产品编码"
          />
        </el-form-item>
        <el-form-item label="工序">
          <el-select
            v-model="queryForm.process"
            placeholder="请选择工序"
            clearable
          >
            <el-option
              v-for="item in processOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="检验类型">
          <el-select
            v-model="queryForm.type"
            placeholder="请选择类型"
            clearable
          >
            <el-option label="首检" value="first" />
            <el-option label="巡检" value="patrol" />
            <el-option label="末检" value="final" />
          </el-select>
        </el-form-item>
        <el-form-item label="检验日期">
          <el-date-picker
            v-model="queryForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item label="检验结果">
          <el-select
            v-model="queryForm.result"
            placeholder="请选择结果"
            clearable
          >
            <el-option label="合格" value="qualified" />
            <el-option label="不合格" value="unqualified" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table :data="tableData" border style="width: 100%">
        <el-table-column prop="inspectionNo" label="检验单号" width="160" />
        <el-table-column prop="workOrderNo" label="工单号" width="160" />
        <el-table-column prop="productName" label="产品名称" min-width="120" />
        <el-table-column prop="process" label="工序" width="100" />
        <el-table-column prop="type" label="检验类型" width="100">
          <template #default="scope">
            {{ getInspectionTypeText(scope.row.type) }}
          </template>
        </el-table-column>
        <el-table-column prop="inspector" label="检验人" width="100" />
        <el-table-column prop="inspectionTime" label="检验时间" width="160" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="scope">
            <el-tag :type="getStatusType(scope.row.status)">
              {{ getStatusText(scope.row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="scope">
            <el-button link type="primary" @click="handleDetail(scope.row)"
              >详情</el-button
            >
            <el-button
              v-if="scope.row.status === 'unqualified'"
              link
              type="warning"
              @click="handleRework(scope.row)"
            >
              返工
            </el-button>
            <el-button link type="primary" @click="handlePrint(scope.row)"
              >打印</el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 返工处理对话框 -->
    <el-dialog v-model="reworkDialogVisible" title="返工处理" width="500px">
      <el-form ref="reworkFormRef" :model="reworkForm" label-width="100px">
        <el-form-item label="不合格原因" prop="reason">
          <el-input
            v-model="reworkForm.reason"
            type="textarea"
            rows="3"
            placeholder="请输入不合格原因"
          />
        </el-form-item>
        <el-form-item label="处理方法" prop="method">
          <el-input
            v-model="reworkForm.method"
            type="textarea"
            rows="3"
            placeholder="请输入处理方法"
          />
        </el-form-item>
        <el-form-item label="改进建议" prop="suggestion">
          <el-input
            v-model="reworkForm.suggestion"
            type="textarea"
            rows="3"
            placeholder="请输入改进建议"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="reworkDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleReworkSubmit">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from "vue";
import { ElMessage } from "element-plus";

// 工序选项
const processOptions = [
  { value: "P001", label: "机加工" },
  { value: "P002", label: "焊接" },
  { value: "P003", label: "装配" },
  { value: "P004", label: "表面处理" },
  { value: "P005", label: "热处理" },
];

// 查询表单
const queryForm = reactive({
  workOrderNo: "",
  productCode: "",
  process: "",
  type: "",
  dateRange: [],
  result: "",
});

// 表格数据
const tableData = ref([
  {
    inspectionNo: "IPQC202403150001",
    workOrderNo: "WO202403150001",
    productCode: "PC100234",
    productName: "高精度轴承组件",
    process: "机加工",
    type: "first",
    batchNo: "BT20240315001",
    inspector: "张三",
    inspectionTime: "2024-03-15 09:30:00",
    status: "qualified",
    remark: "首检合格",
  },
  {
    inspectionNo: "IPQC202403150002",
    workOrderNo: "WO202403150002",
    productCode: "PC100567",
    productName: "控制阀体",
    process: "焊接",
    type: "patrol",
    batchNo: "BT20240315002",
    inspector: "李四",
    inspectionTime: "2024-03-15 10:45:00",
    status: "unqualified",
    remark: "发现焊接缺陷，需要返工",
  },
  {
    inspectionNo: "IPQC202403150003",
    workOrderNo: "WO202403150003",
    productCode: "PC100789",
    productName: "电机组件",
    process: "装配",
    type: "final",
    batchNo: "BT20240315003",
    inspector: "王五",
    inspectionTime: "2024-03-15 14:20:00",
    status: "processing",
    remark: "末检进行中",
  },
]);

// 分页数据
const total = ref(3);
const currentPage = ref(1);
const pageSize = ref(10);

// 返工表单数据
const reworkDialogVisible = ref(false);
const reworkFormRef = ref(null);
const reworkForm = reactive({
  reason: "",
  method: "",
  suggestion: "",
});

// 检验类型显示
const getInspectionTypeText = (type) => {
  const typeMap = {
    first: "首检",
    patrol: "巡检",
    final: "末检",
  };
  return typeMap[type];
};

// 状态显示
const getStatusType = (status) => {
  const statusMap = {
    processing: "warning",
    qualified: "success",
    unqualified: "danger",
  };
  return statusMap[status];
};

const getStatusText = (status) => {
  const statusMap = {
    processing: "检验中",
    qualified: "合格",
    unqualified: "不合格",
  };
  return statusMap[status];
};

// 处理函数
const handleSearch = () => {
  ElMessage.success("查询成功");
};

const handleReset = () => {
  Object.keys(queryForm).forEach((key) => {
    queryForm[key] = "";
  });
  queryForm.dateRange = [];
};

const handleCreate = () => {
  ElMessage.success("新建检验");
};

const handlePlan = () => {
  ElMessage.success("打开检验计划");
};

const handleDetail = (row) => {
  ElMessage.success(`查看详情：${row.inspectionNo}`);
};

const handleRework = (row) => {
  reworkDialogVisible.value = true;
  reworkForm.reason = "";
  reworkForm.method = "";
  reworkForm.suggestion = "";
};

const handleReworkSubmit = () => {
  ElMessage.success("提交成功");
  reworkDialogVisible.value = false;
};

const handlePrint = (row) => {
  ElMessage.success(`打印报告：${row.inspectionNo}`);
};

const handleSizeChange = (val) => {
  pageSize.value = val;
};

const handleCurrentChange = (val) => {
  currentPage.value = val;
};
</script>

<style scoped>
.process-control-container {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.search-form {
  margin-bottom: 20px;
}

.pagination {
  margin-top: 20px;
  text-align: right;
}

.dialog-footer {
  padding-top: 20px;
  text-align: right;
}
</style>
